<template>
  <div class="bookings-container">
    <el-card class="bookings-card">
      <template #header>
        <div class="card-header">
          <span>我的预约</span>
        </div>
      </template>
      <el-table :data="bookings" style="width: 100%">
        <el-table-column prop="venueName" label="场地" width="120" />
        <el-table-column prop="coachName" label="教练" width="120" />
        <el-table-column prop="courseName" label="课程" width="150" />
        <el-table-column prop="price" label="金额" width="100">
          <template #default="scope">¥{{ scope.row.price }}</template>
        </el-table-column>
        <el-table-column prop="time" label="预约时间" width="180" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag v-if="scope.row.status === 1" type="success">已预约</el-tag>
            <el-tag v-else-if="scope.row.status === 0" type="info">已取消</el-tag>
            <el-tag v-else type="warning">未知</el-tag>
          </template>
        </el-table-column>
      </el-table>
      <div v-if="!bookings.length" class="empty-state">
        <el-empty description="暂无预约记录" />
      </div>
    </el-card>
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { bookingApi } from '@/api'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
const bookings = ref([])
const router = useRouter()
const fetchBookings = async () => {
  const userId = localStorage.getItem('userId')
  if (!userId) {
    ElMessage.error('未获取到用户ID，请重新登录')
    router.push('/login')
    return
  }
  try {
    const res = await bookingApi.getUserBookings(userId)
    bookings.value = (res.data.bookings || []).map(item => ({
      ...item,
      venueName: item.venueName || item.venue?.name || '',
      coachName: item.coachName || item.coach?.name || '',
      courseName: item.courseName || item.course?.title || '',
      price: item.price,
      time: item.time,
      status: item.status
    }))
  } catch (e) {
    ElMessage.error('获取预约数据失败')
  }
}
onMounted(fetchBookings)
</script>
<style scoped>
.bookings-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 40px 0;
  display: flex;
  justify-content: center;
  align-items: flex-start;
}
.bookings-card {
  width: 900px;
  border-radius: 18px;
  box-shadow: 0 8px 32px rgba(102,126,234,0.10);
  background: rgba(255,255,255,0.98);
}
.card-header {
  font-size: 1.3rem;
  font-weight: 700;
  color: #3a3a6a;
  text-align: center;
}
.empty-state {
  padding: 40px 0;
}
</style> 